package com.zg.ktcomwan.base

import com.zg.ktcomwan.data.BaseResp
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext

/***
 * @Author:      zxd
 * @CreateDate:  2021/6/3 14:02
 * @Description:
 */
open class BaseRepository {

    suspend fun <T : Any> apiCall(call: suspend () -> BaseResp<T>): BaseResp<T> {
        return withContext(IO) { call.invoke() }.apply {
            // 特殊处理
            when (errorCode) {
                99999 -> throw Exception(errorMsg)
                80000 -> throw TokenInvalidException()
            }
        }
    }

    class TokenInvalidException(msg: String = "token invalid") : Exception(msg)
}